# valgrind
# valgrind --tool=memcheck --show-reachable=yes --leak-check=full --log-file=memcheck.txt -v --error-limit=no [excute]
import os
import sys

# create tmp file
def CreateTmp():
    print 'Create tmp file'
    os.system("touch ./seclink")
    os.system("touch ./tioctl.h")

root_dir = '../../../../'
security_api_dir = root_dir + 'framework/src/security/'
seclink_dir = root_dir + 'framework/src/seclink/'
seclink_inc_dir = root_dir + 'os/include/tinyara/'
system_inc_dir = root_dir + 'os/include/tinyara/fs/'
seclink_drv_path = root_dir + 'os/drivers/seclink/'
se_inc_dir = root_dir + "os/se/virtual/"
testcase_dir = root_dir + "apps/examples/testcase/"
sectest_utc_dir = testcase_dir + 'ta_tc/security/utc/'

main_file = ['./main.c']
#security_test_files = Glob('../security_api/*.c')
testcase_files = [testcase_dir + 'tc_main.c']
security_test_files = Glob(testcase_dir + '/ta_tc/security/utc/*.c')
security_api_files = Glob(security_api_dir + '*.c')
seclink_client_files = Glob(seclink_dir + '*.c')
seclink_drv_filename = ['seclink_drv_linux.c', 'seclink_drv_common.c', \
                        'seclink_drv_auth.c', 'seclink_drv_key.c',\
                        'seclink_drv_ss.c', 'seclink_drv_crypto.c']
seclink_drv_files = []
for file in seclink_drv_filename:
    seclink_drv_files.append(seclink_drv_path + file)

se_files = Glob(se_inc_dir + '*.c')

src_files = security_test_files + security_api_files + seclink_client_files +\
            seclink_drv_files + se_files + main_file + testcase_files
# Create built files to get gcov informations

with open("object_list.txt", "w") as f:
    for t in src_files:
        f.write(str(t))
        f.write('\n')

env = Environment(ENV = os.environ)

##########################################
# Setup Linux Environment to build
##########################################
# Create directory
tmp_sys_dir = './include/sys/'
tmp_tinyara_dir = './include/tinyara/'
tmp_serial_dir = './include/tinyara/serial/'

env.Command('file1.out', '', [Mkdir(tmp_sys_dir)])
env.Command('file2.out', '', [Mkdir(tmp_tinyara_dir)])
env.Command('file3.out', '', [Mkdir(tmp_serial_dir)])

tiny_copy_files = ['seclink.h', 'seclink_drv.h', 'security_hal.h']
sys_copy_files = ['ioctl.h']
dirs_create = [tmp_sys_dir, tmp_tinyara_dir, tmp_serial_dir]

# Copy files
env.Command(tmp_tinyara_dir + 'config.h', './config.h', [Copy(tmp_tinyara_dir+'config.h', './config.h')])
for t in tiny_copy_files:
    print("Copy command: " + t)
    print("src: " + seclink_inc_dir + t)
    print("dest: " + tmp_tinyara_dir + t)
    env.Command(tmp_tinyara_dir + t, seclink_inc_dir + t, [Copy(tmp_tinyara_dir+t, seclink_inc_dir+t)])
    env.Clean(tmp_tinyara_dir, os.path.join(tmp_tinyara_dir, t))

for t in sys_copy_files:
    env.Command(t, '', [Copy(tmp_sys_dir + t, system_inc_dir + t)])
    env.Clean(tmp_sys_dir, os.path.join(tmp_sys_dir, t))

env.Command('file4.out', '', [CreateTmp()])
env.Command('file5.out', './tioctl.h', [Copy(tmp_serial_dir + 'tioctl.h', 'tioctl.h')])
env.Clean('./include/', './include/')
env.Clean('./seclink', './seclink')
env.Clean('./tioctl.h', './tioctl.h')


##################################################
# Build Configuration
##################################################
env.Append(CFLAGS=['-g'])
env.Append(CFLAGS=['-fprofile-arcs'])
env.Append(CFLAGS=['-ftest-coverage'])

#env.Append(CFLAGS=['-I./include', '-I./third_party'])
framework_inc = root_dir + 'framework/include/'

print("inc path: " + framework_inc)

env.Append(CFLAGS=['-I./include', '-I'+framework_inc])
env.Append(CFLAGS=['-I'+sectest_utc_dir, '-I'+testcase_dir])

env.AppendUnique(CPPDEFINES = ['LINUX'])
libraries = ['curl', 'pthread', 'gcov']
#lib_path = ['/usr/lib/x86_64-linux-gnu/']

env.Program('sec_test', src_files, LIBS=libraries)
